﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.Odbc;

namespace Test_crystal_report
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

      

        private void crystalReportViewer1_Load_1(object sender, EventArgs e)
        {
            ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;

            cryRpt.Load("build up report by Date.rpt");
            //
            //   cryRpt.Load("D:\\Google Drive\\Google Drive\\Code\\Test\\Test_crystal report\\Test_crystal report\\Test_crystal report\\CrystalReport2.rpt");

            crConnectionInfo.ServerName = "SJ";
            crConnectionInfo.DatabaseName = "EICS";
            crConnectionInfo.UserID = "sa";
            crConnectionInfo.Password = "02340234";
    

            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }



            /*


            //------------------------------------------------------------------
            // กำหนด พารามิเตอร์ตัวแรก UserID

            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();

            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = "user1";
            crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["@UserID"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            //   -------------------------------------
            // กำหนด พารามิเตอร์ตัวที่ 2 Password

            crParameterDiscreteValue.Value = "user1";
            crParameterFieldDefinition = crParameterFieldDefinitions["@Password"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;


            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            //---------------------------------------------------------------
            //bacup ไว้แบบ Full


            //ParameterFieldDefinitions crParameterFieldDefinition2s2;
            //ParameterFieldDefinition crParameterFieldDefinition2;
            //ParameterValues crParameterValues2 = new ParameterValues();

            //ParameterDiscreteValue crParameterDiscreteValue2 = new ParameterDiscreteValue();

            //crParameterDiscreteValue2.Value = "user1";
            //crParameterFieldDefinition2s2 = cryRpt.DataDefinition.ParameterFields;
            //crParameterFieldDefinition2 = crParameterFieldDefinition2s2["@UserID"];
            //crParameterValues2 = crParameterFieldDefinition2.CurrentValues;

            //crParameterValues2.Clear();
            //crParameterValues2.Add(crParameterDiscreteValue2);
            //crParameterFieldDefinition2.ApplyCurrentValues(crParameterValues2);

            //---------------------------------------------------------------
 */

            crystalReportViewer1.ReportSource = cryRpt;
            crystalReportViewer1.Refresh();
        }
    }
}
